#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

void lexical(FILE *fp) {
    char x = getc(fp);

    num = 0;
    strcpy(id, "\0");

    //process x
    //process each regex here
    //store the result in global variables id, num, or token
    //in case of backtrack, use
    //ungetc(x, fp);

    while(x==' '||x=='\t'||x=='\n')
	{
		x=getc(fp);
	}
	if(x=='\0')
	{
		x=getc(fp);
	}
	if((x>='a'&& x<='z')||(x>='A'&&x<='Z'))
    {
        int i=0;
        do{
            id[i]=x;
            i++;
            x = getc(fp);
        }while((x>='0'&&x<='9')||(x>='a'&& x<='z')||(x>='A'&&x<='Z'));
        ungetc(x,fp);
        if(id=="for")
            token=1;
        else if(id=="if")
            token=2;
        else if(id=="then")
            token=3;
        else if(id=="else")
            token=4;
        else if(id=="while")
            token=5;
        else if(id=="do")
            token=6;
        else
            token=10;
    }
    else if(x>='0'&&x<='9')
    {
        do{
            num=num*10+(x-'0');
            x = getc(fp);
        }while(x>='0'&&x<='9');
        ungetc(x,fp);
        token=11;
    }
    else if(x=='+')
    {
        strcpy(id,"+");
        token=13;
    }
    else if(x=='-')
    {
        strcpy(id,"-");
        token=14;
    }
     else if(x=='*')
    {
        strcpy(id,"*");
        token=15;
    }
    else if(x=='/')
    {
        strcpy(id,"/");
        token=16;
    }
    else if(x==':')
    {
        x=getc(fp);
        if(x=='=')
        {
            strcpy(id,":=");
            token=18;
        }
        else
        {
            strcpy(id,":");
            token=17;
            ungetc(x,fp);
        }
    }
    else if(x=='<')
    {
        x = getc(fp);
        if(x=='>')
        {
            strcpy(id,"<>");
            token=21;
        }
        else if(x=='=')
        {
            strcpy(id,"<=");
            token=22;
        }
        else
        {
            strcpy(id,"<");
            token=20;
            ungetc(x,fp);
        }
    }
    else if(x=='>')
    {
        x=getc(fp);
        if(x=='=')
        {
            strcpy(id,">=");
            token=24;
        }
        else
        {
            strcpy(id,">");
            token=23;
            ungetc(x,fp);
        }
    }
    else if(x=='=')
    {
        strcpy(id,"=");
        token=25;
    }
    else if(x==';')
    {
        strcpy(id,";");
        token=26;
    }
    else if(x=='(')
    {
        strcpy(id,"(");
        token=27;
    }
    else if(x==')')
    {
        strcpy(id,")");
        token=28;
    }
    else if(x=='#')
    {
        strcpy(id,"#");
        token=0;
    }
    else
        token=-1;
}

int main(int argc, const char *argv[])
{
        if(argc < 2) {
                printf("insufficient args\n");
                return 1;
        }
        FILE *fp = fopen(argv[1], "r");
        do {
                lexical(fp);
                switch(token) {
                        case 10: printf("(id, %s)\n", id); break;
                        case 11: printf("(num, %d)\n", num); break;
                        case -1: printf("error!\n"); break;
                        default: printf("(%d, %s)\n", token, id);
                }
                strcpy(id,"\0");
        } while(token != 0);


        fclose(fp);
        return 0;
}


